//
//  UMAnalytics.m
//  WonderMobSDK
//
//  Created by zero on 2020/3/4.
//

#import "UMAnalytics.h"
#import "WDGroMoreHeader.h"

@interface UMAnalytics()


@end

@implementation UMAnalytics

+ (instancetype)manager {
    static UMAnalytics *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[self alloc] init];
    });
    return _manager;
}

- (void)initUMSDK{
    if([WDGroMoreJosnManager UMAppkeyForSDKConfigFile]&&[WDGroMoreJosnManager UMAppkeyForSDKConfigFile].length>8){
        [UMConfigure initWithAppkey:[WDGroMoreJosnManager UMAppkeyForSDKConfigFile] channel:[WDGroMoreJosnManager bmsChannelForSDKConfigFile]];
    }
#ifdef DEBUG
    [UMConfigure setLogEnabled:YES];
    NSLog(@"友盟测试设备号：%@",[UMConfigure deviceIDForIntegration]);
    [UMCommonLogManager setUpUMCommonLogManager];
#endif
}

+ (void)umcEvent:(NSString *)event params:(NSDictionary<NSString *, NSString *> *)params {
    if (![event isKindOfClass:[NSString class]] || event.length == 0) {
        NSLog(@"[UMAnalytics] 埋点失败：事件名无效");
        return;
    }

    if ([params isKindOfClass:[NSDictionary class]] && params.count > 0) {
        // 防御：友盟要求 key/value 都为 NSString
        NSMutableDictionary *validParams = [NSMutableDictionary dictionary];
        [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            if ([key isKindOfClass:[NSString class]] && [obj isKindOfClass:[NSString class]]) {
                validParams[key] = obj;
            }
        }];

        if (validParams.count > 0) {
            [MobClick event:event attributes:validParams];
            return;
        }
    }

    // 无合法参数，调用无参数版本
    [MobClick event:event];
}

@end
